﻿using System;
using MockDataTransferObject;
using MockBusiness;
using MockCommon;
using System.Web.UI.WebControls;
using System.Globalization;

namespace Presentation.AdminPresentation
{
    public partial class AddConsultant : System.Web.UI.Page
    {
        private RoleBusiness _roleBusiness;
        private ConsultantBusiness _consultantBusiness;
        private UserBusiness _userBusiness;
        private Label _warningLabel;
        private Label _errorLabel;
        private Label _successLabel;
        private TextBox _firstNameTextBox;
        private TextBox _lastNameTextBox;
        private TextBox _birthdateTextBox;
        private TextBox _telephoneTextBox;
        private TextBox _addressTextBox;
        private RadioButton _maleRadio;
        private CheckBox _activeCheckBox;
        private Button _addButton;
        private Button _resetButton;

        protected void Page_Init(object sender, EventArgs e)
        {
            try
            {
                _roleBusiness = RoleBusiness.RetrieveInstance();
                _consultantBusiness = ConsultantBusiness.RetrieveInstance();
                _userBusiness = UserBusiness.RetrieveInstance();
                _warningLabel = (Label)Master.FindControl("WarningLabel");
                _errorLabel = (Label)Master.FindControl("ErrorLabel");
                _successLabel = (Label)Master.FindControl("SuccessLabel");
                _firstNameTextBox = (TextBox)AddConsultantUserControl.FindControl("FirstNameTextBox");
                _lastNameTextBox = (TextBox)AddConsultantUserControl.FindControl("LastNameTextBox");
                _birthdateTextBox = (TextBox)AddConsultantUserControl.FindControl("BirthDateTextBox");
                _telephoneTextBox = (TextBox)AddConsultantUserControl.FindControl("TelephoneTextBox");
                _addressTextBox = (TextBox)AddConsultantUserControl.FindControl("AddressTextBox");
                _maleRadio = (RadioButton)AddConsultantUserControl.FindControl("MaleRadio");
                _activeCheckBox = (CheckBox)AddConsultantUserControl.FindControl("ActiveCheckBox");           
                _addButton = (Button)AddConsultantUserControl.FindControl("AddButton");
                _resetButton = (Button)AddConsultantUserControl.FindControl("ResetButton");
                _addButton.Click += new EventHandler(AddButton_Click);
                _resetButton.Click += new EventHandler(ResetButton_Click);
            }
            catch (MockException exception)
            {
                Application["error"] = exception;
                Response.Redirect("../Error.aspx");
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["user"] != null)
            {
                User user = Session["user"] as User;
                if (user.Role.RoleName != "Admin")
                {
                    Server.Transfer("../NotAuthorized.aspx");
                }
            }
            else
            {
                Response.Redirect("../LogOn.aspx");
            }
            if (IsPostBack)
            {
                _warningLabel.Text = "";
                _errorLabel.Text = "";
                _successLabel.Text = "";
            }
        }

        protected void AddButton_Click(object sender, EventArgs e)
        {
            try
            {
                //Constant
                string consultantRoleName = "Consultant";
                string successFormat = "Create consultant with user {0} successfully!";
                //Get form
                string firstName = _firstNameTextBox.Text;
                string lastName = _lastNameTextBox.Text;
                string username = _userBusiness.GenerateUserName(firstName, lastName);
                string password = UserBusiness.GeneratePassword();
                string goodFirstName = UserBusiness.FormatName(firstName);
                string goodLastName = UserBusiness.FormatName(lastName);
                string telephone = _telephoneTextBox.Text;
                string address = _addressTextBox.Text;
                string successText = String.Format(CultureInfo.CurrentCulture, successFormat, username);
                object roleId = _roleBusiness.GetRoleIdByRoleName(consultantRoleName);
                DateTime birthdate = Convert.ToDateTime(_birthdateTextBox.Text, CultureInfo.CurrentCulture);
                bool isActive = _activeCheckBox.Checked;
                bool gender = _maleRadio.Checked ? true : false;
                Guid userId = Guid.NewGuid();
                Guid consultantId = Guid.NewGuid();
                //Create role
                Role role = new Role();
                role.RoleId = (Guid)roleId;
                //Create user
                User user = new User();
                user.UserId = userId;
                user.Role = role;
                user.UserName = username;
                user.Password = password;
                user.IsActive = isActive;
                //Create consultant
                Consultant consultant = new Consultant();
                consultant.ConsultantId = consultantId;
                consultant.FirstName = goodFirstName;
                consultant.LastName = goodLastName;
                consultant.BirthDate = birthdate;
                consultant.Telephone = telephone;
                consultant.Address = address;
                consultant.Gender = gender;   
                consultant.User = user;
                consultant.Telephone = telephone;
                consultant.Address = address;     
                //Create
                _consultantBusiness.Insert(consultant);
                _successLabel.Text = successText;
                //Finally reset
                Reset();
            }
            catch (NullReferenceException exception)
            {
                _errorLabel.Text = exception.Message;
            }
            catch (ArgumentException exception)
            {
                _errorLabel.Text = exception.Message;
            }
            catch (FormatException exception)
            {
                _errorLabel.Text = exception.Message;
            }
            catch (MockException exception)
            {
                _errorLabel.Text = exception.ExceptionMessage;
            }         
        }

        protected void ResetButton_Click(object sender, EventArgs e)
        {
            Reset();
        }

        private void Reset()
        {
            _firstNameTextBox.Text = "";
            _lastNameTextBox.Text = "";
            _birthdateTextBox.Text = "";
            _maleRadio.Checked = true;
            _addressTextBox.Text = "";
            _telephoneTextBox.Text = "";
            _activeCheckBox.Checked = true;
        }
    }
}